/**
 * Created by zzy on 2017/1/13.
 */
var $ = require('jquery');
var $alert = require('libs/alert');
var $form = require('js-path/formValide.js');
var $form2 = require('js-path/formValideNotSub.js');
var col = 5;
var mms = 3;
$(function(){
    pageInit();
    mainFormValidInit();
});
function pageInit() {
    var id = $.getUrlParam('id');
    loadAndInit(id);
    $("#main_form input[name=systemId]").on("change",function () {
        var systemId = $(this).val();
        resourceSimpleQueryAjax(systemId);
    })
}
function resourceSimpleQueryAjax(systemId) {
    $.ajax({
        url:"api/v1/p/mms/authResource/queryResource",
        type:"post",
        data:{
            systemId:systemId
        },
        dataType:"json",
        success:function (result) {
            if(result.code==200){
                resourceInit(result.data);
            }else{
                $alert(result.message);
            }
        }
    })
}
function loadAndInit(id) {
    $.ajax({
        url:'api/v1/p/mms/authAuthority/load',
        type:'post',
        data:{
            id:id
        },
        dataType:"json",
        success:function (result) {
            if(result.code==200){
                var data = result.data;
                dataInit(data);
            }else{
                $alert(result.message);
            }
        }
    })
}
function dataInit(data) {
    $("#id").val(data.id);
    $("#authority_mark").val(data.authorityMark);
    $("#authority_name").val(data.authorityName);
    $("#authority_desc").val(data.authorityDesc);
    $("#message").val(data.message);
    $("#issys").val(data.issys);
    $.each($("#main_form input[name=systemId]"),function (k,o) {
        if($(o).val()==data.systemId){
            $("#main_form input[name=systemId]").eq(k).attr("checked","checked");
            return;
        }
    });
    $.ajax({
        url:"api/v1/p/mms/authResource/queryResource",
        type:"post",
        data:{
            systemId:data.systemId
        },
        dataType:"json",
        success:function (result) {
            if(result.code==200){
                resourceInit(result.data,data.list);
            }else{
                $alert(result.message);
            }
        }
    })
}
function resourceInit(data,defaultCheckedValue) {
    var resourceIds = new Array();
    if(defaultCheckedValue!=undefined){
        $.each(defaultCheckedValue,function (k,o) {
            resourceIds.push(o.resourceId);
        })
    }
    $("#div_resource").html("");
    var len = data.length;
    if(len!=0){
        var divArray = new Array(Math.floor((len-1)/col)+1);
        $.each(divArray,function (k,o) {
            divArray[k] = $("<div style='padding-left: 15px'></div>");
        });
        $.each(data,function (k,o) {
            if($.inArray(data[k].id, resourceIds)==-1){
                divArray[Math.floor(k/col)].append("<input type='checkbox' name='resourceId' value='"+data[k].id+"'><span style='margin-right: 10px'>"+data[k].name+"</span>");
            }else{
                divArray[Math.floor(k/col)].append("<input type='checkbox' name='resourceId' value='"+data[k].id+"' checked><span style='margin-right: 10px'>"+data[k].name+"</span>");
            }
        });
        $.each(divArray,function (k,o) {
            $("#div_resource").append(divArray[k]);
        });
    }
}
function customValid() {
    var resource = true;
    var len = $("#main_form input[type=checkbox]:checked").length;
    if(len==0){
        $("#div_resource").parent().parent().addClass("has-error");
        $("#div_resource").parent().next("div").html('<p id="authority-error" class="error">资源不能为空</p>');
        resource = false;
    }
    return resource;
}
function mainFormValidInit(){
    $form({selector:'#main_form'},{
        authorityMark:{required:true},authorityName:{required:true},
        issys:{required:true},systemId:{required:true}
    },{authorityMark:{required:'权限标示不能为空'},authorityName:{required:'权限名称不能为空'},
        issys:{required:'是否系统级别不能为空'},systemId:{required:'所属系统不能为空'}
    },function () {
        if(!customValid()){
            return false;
        }
        var json = $('#main_form').serializeObject();
        var resourceIds = json.resourceId;
        var ids = new Array();
        if(typeof resourceIds == "object"){
            $.each(resourceIds,function (k,o) {
                ids.push({"id":o});
            });
        }else{
            ids.push({"id":resourceIds});
        }
        json.list = ids;
        delete json.resourceId;
        json.systemId = $("#main_form input[type=radio]:checked").val();
        return json;
    },function (result) {
        if(result.code == 200){
            window.location.href="authority-manage";
        }else{
            $alert(result.message);
        }
    })
}